package nl.hva.three2learn.ui
{
	import flash.display.Graphics;
	import flash.geom.Point;

	public class Line
	{
		private var _points:Array;
		private var _animate:Boolean;
	
		private var _currentIndex:int;
	
		public function Line(points:Array = null, animate:Boolean = false)
		{
			_points = points || [ ];
			_animate = animate;
			
			_currentIndex = 0;
		}
		
		public function draw(graphics:Graphics):void
		{
			if (_points.length > 1)
			{
				var start:Object = _points[0];
				
				graphics.moveTo(start.x, start.y);
		
				while (drawNextPoint(graphics));
				
				// Reset the index for the next draw call
				_currentIndex = 0;
			}
		}
		
		public function drawNextPoint(graphics:Graphics):Boolean
		{
			if (_currentIndex >= _points.length)
				return false;
		
			var p:Object = _points[_currentIndex];
		
			if (_currentIndex++ === 0)
				graphics.moveTo(p.x, p.y);
			else
				graphics.lineTo(p.x, p.y);
		
			return true;
		}
		
		public function addPoint(point:Point):void
		{
			_points.push({ x: point.x, y: point.y });
		}
		
		public function get points():Array
		{
			return _points;
		}
	}	
}
